package v3.object.command.list;

import galaxy.obj.Point;

import java.text.DecimalFormat;
import java.util.Map;

import text.TextUtil;
import v3.object.STcfg;
import v3.object.Screen;
import v3.object.command.aCommand;
import v3.object.command.iCommand;
import v3.object.command.servoCommand.OnScreen;
import v3.object.ship.Ship;
import v3.object.space.Sector;
import v3.object.space.SpaceObjType;
import v3.object.space.Universe;

public class ShortSensor extends aCommand
{

	public static final String SEP= " ";//String.valueOf(Character.toChars(183)) ;
	
	public ShortSensor()
	{
		super();
			commandName="SHORT RANGE SENSOR";
	}

	@Override
	public boolean realExecute(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		int sensorSize = ship.getSSsize();
		Point shipPosition = ship.getPosition();
		
		Screen screen = getShortSensorScreen(shipPosition, sensorSize, universe,shipPosition);	
		
//		if(cfg.isVerbose())
//			System.out.println(screen);
		
		iCommand c = new OnScreen(commandName, screen.toString());
		c.execute(cfg, universe, ship, commands, input);
		return true;
	}

	public Screen getShortSensorScreen(Point shipPosition, int sensorSize, Universe universe, Point markPoint )
	{
		
		int x1=shipPosition.getiX()-sensorSize;
		int x2=shipPosition.getiX()+sensorSize+1;
		int y1=shipPosition.getiY()-sensorSize;
		int y2=shipPosition.getiY()+sensorSize+1;
		
		int len = (""+(x2)).length();
		
		Screen screen = new Screen(sensorSize*2+2+len, sensorSize*8+5+len);
		screen.fill(SEP);
		
		//Create rows 
		String mask = "0000000".substring(0, len);
		DecimalFormat f = new DecimalFormat(mask);
		for(int i=0;i<len;i++)
		{
			screen.printAt(i,0,mask.replace("0", " ")+" ");
			int k=len+1;
			for(int j=x1;j<x2;j++)
			{
				if(j<0 || j>universe.getUniverseSize())
				{
					screen.printAt(i, k, "---|");
					k=k+4;
				}
				else
				{
					String st = f.format(j);
					st=st.substring(i, i+1);
					screen.printAt(i, k++," ");
					screen.printAt(i, k++,st);
					screen.printAt(i, k++," ");
					screen.printAt(i, k++,"|");
				}
				
			}
		}
		
		//Create row of separation
		StringBuilder sb = new StringBuilder();
		for( int i=0; i< screen.rows;i++)
		{
			sb.append(SEP);
		}
		screen.printAt(len, 0, sb);
		
		//create Square of SS
		int yy = len+1;
		for(int y=y1;y<y2;y++)
		{
			
			
			if(y<0 || y>=universe.getUniverseSize())
				screen.printAt(yy,0,mask.replace("0", "-")+"  ");
			else
				screen.printAt(yy, 0,f.format(y)+SEP);
			int xx =len+1;
			for(int x=x1;x<x2;x++)
			{

				Sector s = universe.getSector(x, y);
				String info=" "+String.valueOf(Character.toChars(183))+" ";
				if(!s.isEmpty())
				{
					info =s.getInfoString(SpaceObjType.Star,SpaceObjType.Enemy,SpaceObjType.StarBase);
				}
				
				if(x<0 || y<0 ||x>=universe.getUniverseSize() || y>=universe.getUniverseSize())
					screen.printAt(yy, xx, "---"+SEP);
				else
					screen.printAt(yy, xx,info.replace(";", "")+SEP);/**/
			xx=xx+4;
			}
			yy++;
		}
		
			int rowE=markPoint.getiX()-x1+len+1;
			int colE=(markPoint.getiY()-y1)*4+len+2;
			screen.printAt(rowE, colE-2, "[");
			screen.printAt(rowE, colE+2, "]");
		
		return screen;
	}

	@Override
	public void getHelp(Ship ship)
	{
		String help = "ShortSensor [ss]\n\n" + "give the information of the current Sector\n" + "Stars Enemy Bases\n" + "Time Cost .01\n" + "Energy Cost "+ship.getEnergyConsume((this.getClass()))+"";
		help = TextUtil.formatHelp(help);
		System.out.println(help);

	}

	@Override
	protected double calculateEnergy(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return ship.getEnergyConsume(this.getClass());
	}

	@Override
	protected void checkInput(String[] input) throws Exception
	{
		
	}

	@Override
	protected boolean isCommandAccetable(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return true;
	}

	@Override
	protected double calculateTime(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return ship.getTimeConsume((this.getClass()));
	}

	@Override
	protected boolean confirmCommand(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return true;
	}
}
